package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.CompanyIndexDao;
import ex.com.jm.food.entity.CompanyProfitSummary;

public class CompanyIndexDaoImpl extends HibernateDaoSupport implements
		CompanyIndexDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<CompanyProfitSummary> getCPS(final String comId,final String s_date,
			final String e_date) {
		// TODO Auto-generated method stub
		Object cps=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from CompanyProfitSummary cps where cps.comId='"+comId+"' ";
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and cps.sDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and cps.sDate<='"+e_date+"'";
				}
				hql+=" group by cps.sDate order by cps.sDate desc";
				Query query=session.createQuery(hql);
				return query.list();
			}
		});
		return (List<CompanyProfitSummary>) cps;
	}

	

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<CompanyProfitSummary> getComCPS(final int initPage,final int pageRow,
			final String comId,final String s_date,final String e_date,final String order,
			final String sort) {
		// TODO Auto-generated method stub
		Object cps=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from CompanyProfitSummary cps where cps.comId='"+comId+"' ";
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and cps.sDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and cps.sDate<='"+e_date+"'";
				}
				hql+=" group by cps.sDate ";
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					hql+=" order by "+order+" "+sort;
				}else {
					hql+=" order by cps.sDate desc";
				}
				
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<CompanyProfitSummary>) cps;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int cpsCount(final String comId,final String s_date,final String e_date) {
		// TODO Auto-generated method stub
		int  count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from CompanyProfitSummary cps where cps.comId='"+comId+"' ";
				if(s_date!=null&&!s_date.equals("")){
					hql+=" and cps.sDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and cps.sDate<='"+e_date+"'";
				}
				hql+=" group by cps.sDate order by cps.sDate desc";
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

}
